package sourceapi.client;

import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.json.client.*;
import com.google.gwt.user.client.ui.Hyperlink;

public class MenuFunction extends Hyperlink implements Comparable<MenuFunction>, HasMouseOverHandlers, HasMouseOutHandlers, MouseOutHandler, MouseOverHandler  {
	
	public int Id;
	public String Name;
	public String Description;
	
	public MenuFunction( int id, JSONArray JsonObject ){	
		this.Id = id;
		this.Name = JsonObject.get(0).isString().stringValue();
		this.Description = JsonObject.get(1).isString().stringValue();
		
		this.addMouseOutHandler( this );
		this.addMouseOverHandler( this );
		
		this.UpdateHtml();
	}
	
	public Hyperlink GetClean(){
		
		return new Hyperlink( this.Name, "f:" + this.Name );
		
	}
	
	private void UpdateHtml(){
		//TODO: On last item of the menu, it should not have the "more"
		this.setHTML( "<img src='images/" + ("more") + "sub.gif' style='vertical-align: bottom'> " + this.Name + "<br>");
		this.setTargetHistoryToken( "f:" + this.Name );
	}
	
	public boolean SearchResult( String search ){
		return this.Name.toLowerCase().contains( search ) ||  this.Description.toLowerCase().contains( search );
	}

	public int compareTo(MenuFunction o) {
		return this.Name.compareTo( o.Name );
	}

	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public void onMouseOut(MouseOutEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMouseOver(MouseOverEvent event) {
		// TODO Auto-generated method stub
		
	}
	
}
